package fr.emse.tatiana.replayable.analysis.graph;

import java.util.List;
import java.util.Vector;

import org.eclipse.draw2d.ConnectionLayer;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.LineBorder;
import org.eclipse.draw2d.ShortestPathConnectionRouter;
import org.eclipse.draw2d.Viewport;
import org.eclipse.draw2d.XYLayout;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.LayerConstants;
import org.eclipse.gef.editparts.AbstractEditPart;
import org.eclipse.gef.editparts.AbstractGraphicalEditPart;

import fr.emse.tatiana.replayable.Anchor;
import fr.emse.tatiana.replayable.analysis.GraphAnalysis;

public class GraphAnalysisPart extends AbstractGraphicalEditPart {

	private GraphAnalysis analysis;
	private Viewport viewport;
	
	public GraphAnalysisPart(GraphAnalysis model) {
		super();
		setModel(model);
		this.analysis = model;
	}
	
	GraphAnalysis getAnalysis() {
		return analysis;
	}

	@Override
	protected IFigure createFigure() {
		IFigure f = new Figure();
		f.setLayoutManager(new XYLayout());
		f.setBorder(new LineBorder(5));
		
		ConnectionLayer connLayer = (ConnectionLayer)getLayer(LayerConstants.CONNECTION_LAYER);
		ShortestPathConnectionRouter router = new ShortestPathConnectionRouter(f);
		router.setSpacing(20);
		connLayer.setConnectionRouter(router);
		
		return f;
	}



	/* (non-Javadoc)
	 * @see org.eclipse.gef.editparts.AbstractEditPart#getModelChildren()
	 */
	@Override
	protected List<Object> getModelChildren() {
		List<Object> l = new Vector<Object>();
		List<Anchor> anchors = new Vector<Anchor>(analysis.getAnchors()); 
		for (int i=0; i<anchors.size(); i++) {
			Anchor a = anchors.get(i);
			if (analysis.hasValueForAnchor(a)) {
				l.add(a);
			}
		}
		return l;
	}

    
    @Override
	protected void refreshVisuals() {
//TODO use layout manager or something else.
		List<AbstractEditPart> children = this.getChildren();
		for (AbstractEditPart child: children) {
			child.refresh();
		}
    }

	@Override
	protected void createEditPolicies() {
		installEditPolicy(EditPolicy.LAYOUT_ROLE, new AppEditLayoutPolicy());
		
		
	}

	
}
